package com.quan.upms.web.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.base.common.base.web.support.BaseResult;
import com.quan.base.common.base.web.support.controller.BaseController;
import com.quan.base.common.utils.Paginator;
import com.quan.upms.annotation.RequiresPermissions;
import com.quan.upms.rpc.api.UpmsDepartmentService;
import com.quan.upms.rpc.model.UpmsDepartment;

@RestController
@RequestMapping(value = "/api/upms/department")
public class UpmsDepartmentController extends BaseController {

	@Autowired
	private UpmsDepartmentService upmsDepartmentService;

	@RequiresPermissions(value="upms:department:add", description="添加部门")
	@PostMapping(value = "/add")
	public Object add(@Valid @RequestBody UpmsDepartment entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		if (null != this.upmsDepartmentService.checkValue("name", entity.getName())) {
			return BaseResult.failure("名称已存在");
		}
		if (null != this.upmsDepartmentService.checkValue("code", entity.getCode())) {
			return BaseResult.failure("编码已存在");
		}
		this.upmsDepartmentService.insertSelective(entity);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:department:delete", description="删除部门")
	@GetMapping(value = "/delete/{id}")
	public Object delete(@PathVariable Integer id) {
		this.upmsDepartmentService.delete(id);
		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:department:update", description="修改部门")
	@PostMapping(value = "/update")
	public Object update(@Valid @RequestBody UpmsDepartment entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
            return BaseResult.failure(bindingResult.getFieldError().getDefaultMessage()) ;
        }
		UpmsDepartment oldCompany = this.upmsDepartmentService.selectById(entity.getId()) ;
		if(null != oldCompany.getName() && !oldCompany.getName().equals(entity.getName())) {
			if(null != this.upmsDepartmentService.checkValue("name", entity.getName())) {
				return BaseResult.failure("名称已存在");
			}
		}
		if(null != oldCompany.getCode() && !oldCompany.getCode().equals(entity.getCode())) {
			if(null != this.upmsDepartmentService.checkValue("code", entity.getCode())) {
				return BaseResult.failure("编码已存在");
			}
		}
		this.upmsDepartmentService.update(entity);

		return BaseResult.ok();
	}

	@RequiresPermissions(value="upms:department:detail", description="查询部门详情")
	@GetMapping(value = "/get/{id}")
	public Object list(@PathVariable Integer id) {
		return this.upmsDepartmentService.selectById(id);
	}
	
	@RequiresPermissions(value="upms:department:list", description="查询部门")
	@GetMapping(value = "/list")
	public Object list(UpmsDepartment entity) {
		return this.upmsDepartmentService.selectPage(new Paginator(this.request), entity);
	}

	@RequiresPermissions(value="upms:department:tree", description="部门树状")
	@GetMapping(value = "/tree")
	public Object tree(UpmsDepartment entity) {
		return this.upmsDepartmentService.tree(entity);
	}
	
	/**
	 * 将用户从部门中移除
	 * @param primaryKey
	 * @return
	 */
	@RequiresPermissions(value="upms:department:remove_user_assign", description="移除该部门成员")
	@GetMapping(value = "/remove_department_user")
	public Object removeDepartmentAssignUser(String ids) {
		this.upmsDepartmentService.removeDepartmentAssignUser(ids);
		return BaseResult.ok();
	}
	
}
